package cn.teduymdj.jxc.controller;

import cn.teduymdj.jxc.myAnnotation.LogAnnotation;
import cn.teduymdj.jxc.pojo.dto.DamageListAddNewDTO;
import cn.teduymdj.jxc.pojo.dto.DamageListGoodsAddNewDTO;
import cn.teduymdj.jxc.pojo.dto.SelectConditionDTO;
import cn.teduymdj.jxc.pojo.entity.Log;
import cn.teduymdj.jxc.pojo.vo.DamageListGoodsListItemVO;
import cn.teduymdj.jxc.pojo.vo.DamageListListItemVO;
import cn.teduymdj.jxc.security.LoginPrincipal;
import cn.teduymdj.jxc.service.DamageListGoodsService;
import cn.teduymdj.jxc.web.JsonResult;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 商品报损商品Controller层
 */
@RestController
@RequestMapping("/damageListGoods")
public class DamageListGoodsController {

    @Autowired
    private DamageListGoodsService damageListGoodsService;

    /**
     * 保存商品报损单信息
     *
     * @param damageList         商品报损单信息实体
     * @param damageListGoodsStr 商品报损商品信息JSON字符串
     * @return
     */
    //日志保存注解,不用管:李柯辰
    @LogAnnotation(content = "保存商品报损单信息",type = Log.INSERT_ACTION)
    @RequestMapping("/save")
    public JsonResult<Void> save(
            @AuthenticationPrincipal LoginPrincipal loginPrincipal,
            DamageListAddNewDTO damageList,
            String damageListGoodsStr) {

        List<DamageListGoodsAddNewDTO> damageListGoodsList =
                JSON.parseArray(damageListGoodsStr, DamageListGoodsAddNewDTO.class);

        damageList.setUserId(loginPrincipal.getId());
        // 保存商品报损清单
        damageListGoodsService.save(damageList, damageListGoodsList);
        // 保存日志
        return JsonResult.ok();
    }

    /**
     * 查询商品报损单
     *
     * @param sTime 开始时间
     * @param eTime 结束时间
     * @return
     */
    @LogAnnotation(content = "查询商品报损单",type = Log.SELECT_ACTION)
    @RequestMapping("/list")
    public JsonResult<List<DamageListListItemVO>> list(@RequestBody SelectConditionDTO selectConditionDTO) {
        List<DamageListListItemVO> damageListList = damageListGoodsService.getDamageList(selectConditionDTO);
        return JsonResult.ok(damageListList);
    }

    /**
     * 查询商品报损单商品信息
     *
     * @param damageListId 商品报损单ID
     * @return
     */
    @LogAnnotation(content = "查询商品报损单商品信息",type = Log.SELECT_ACTION)
    @RequestMapping("/goodsList")
    public JsonResult<List<DamageListGoodsListItemVO>> goodsList(Long damageListId) {
        List<DamageListGoodsListItemVO> damageListGoodsList =
                damageListGoodsService.getDamageListGoodsByDamageListId(damageListId);
        return JsonResult.ok(damageListGoodsList);
    }

}
